/**
 * @(#) EnrollmentServiceImpl.java
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.cms.daoimpl.enrollment;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import in.ac.dei.edrp.cms.dao.enrollment.EnrollmentService;
import in.ac.dei.edrp.cms.daoimpl.universityreservation.UniversityReservationServiceImpl;
import in.ac.dei.edrp.cms.domain.enrollment.EnrollmentInfo;

/**
 * this is Server side Implementation class for Enrollment
 * 
 * @version 1.0 11 MAR 2011
 * @author MOHD AMIR
 */
public class EnrollmentServiceImpl extends SqlMapClientDaoSupport implements
		EnrollmentService {

	/** Creating object of Logger for log Maintenance */
	private static Logger logObj = Logger
			.getLogger(UniversityReservationServiceImpl.class);

	/** Creating object of TransactionTemplate for transaction Management */
	TransactionTemplate transactionTemplate = null;

	/** defining setter method for object of TransactionTemplate */
	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

	/**
	 * This method retrieves Academic Details from database
	 * 
	 * @param regNo
	 *            , registration number
	 * @return academicInfo containing Academic Details
	 */
	@SuppressWarnings("unchecked")
	public List<EnrollmentInfo> getAcademicDetails(String registrationNo) {
		List<EnrollmentInfo> academicInfo = new ArrayList<EnrollmentInfo>();
		try {
			academicInfo = getSqlMapClientTemplate().queryForList(
					"enrollment.getAcademicInfo", registrationNo);
		} catch (Exception e) {
			logObj.error(e.getMessage());
		}
		return academicInfo;
	}

	/**
	 * This method retrieves Contact Details from database
	 * 
	 * @param userId
	 *            , id of user
	 * @return contactInfo containing Contact Details
	 */
	@SuppressWarnings("unchecked")
	public List<EnrollmentInfo> getContactDetails(String userId) {
		List<EnrollmentInfo> contactInfo = new ArrayList<EnrollmentInfo>();
		try {
			contactInfo = getSqlMapClientTemplate().queryForList(
					"enrollment.getContactInfo", userId);
		} catch (Exception e) {
			logObj.error(e.getMessage());
		}
		return contactInfo;
	}

	/**
	 * This method retrieves Personal Details from database
	 * 
	 * @param userId
	 *            , id of user
	 * @return personalInfo containing Personal Details
	 */
	@SuppressWarnings("unchecked")
	public List<EnrollmentInfo> getPersonalDetails(String registrationNo) {
		List<EnrollmentInfo> personalInfo = new ArrayList<EnrollmentInfo>();
		try {
			System.out.println(registrationNo);
			personalInfo = getSqlMapClientTemplate().queryForList(
					"enrollment.getPersonalInfo", registrationNo);
			System.out.println(personalInfo.size());
		} catch (Exception e) {
			System.out.println(e.getMessage());
			logObj.error(e.getMessage());
		}
		return personalInfo;
	}

	/**
	 * This method insert the course group details into database
	 * 
	 * @param personalInfo
	 *            , object of bean class EnrollmentInfo containing personal
	 *            details
	 * @param addressList
	 *            , List of type EnrollmentInfo containing address details
	 * @param academicList
	 *            , List of type EnrollmentInfo containing academic details
	 * 
	 * @return value of type boolean show wheater the record are inserted or not
	 */
	public Boolean updateStudentDetails(final EnrollmentInfo personalInfo,
			final List<EnrollmentInfo> addressList,
			final List<EnrollmentInfo> academicList) {
		String s = (String) transactionTemplate
				.execute(new TransactionCallback() {

					public String doInTransaction(TransactionStatus tStatus) {
						/*
						 * to create a point to which the function revert when
						 * an exception is encountered during the process
						 */
						Object savepoint = null;
						try {
							savepoint = tStatus.createSavepoint();
							getSqlMapClientTemplate().update(
									"enrollment.updatePersonalInfo",
									personalInfo);

							getSqlMapClientTemplate().delete(
									"enrollment.deleteAcademicInfo",
									personalInfo);

							for (int i = 0; i < addressList.size(); i++) {
								EnrollmentInfo addressInfo = addressList.get(i);
								int ru = getSqlMapClientTemplate().update(
										"enrollment.updateContactInfo",
										addressInfo);
								if (ru == 0) {
									getSqlMapClientTemplate().insert(
											"enrollment.setContactInfo",
											addressInfo);
								}
							}
							for (int j = 0; j < academicList.size(); j++) {
								EnrollmentInfo academicInfo = academicList
										.get(j);
								getSqlMapClientTemplate().insert(
										"enrollment.setAcademicInfo",
										academicInfo);
							}
							return "true";
						} catch (Exception e) {
							tStatus.rollbackToSavepoint(savepoint);
							logObj.error(e.getMessage());
							return "false";
						}
					}

				});
		return new Boolean(s);
	}
}